package com.btex.app.treaty;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.btex.app.Config;
import com.btex.app.FuturesKLineActivity;
import com.btex.app.R;
import com.btex.app.adapter.TradesAdapter;
import com.btex.app.entity.TradeInfo;
import com.btex.app.pager.BaseFragment;
import com.btex.app.tools.CookieTool;
import com.btex.app.tools.SharedPreferencesUtil;
import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.PreferencesCookieStore;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author zy zhangyi <zhangyi, 1104745049@QQ.com
 * @version v1.0
 * @project study1
 * @Description
 * @encoding UTF-8
 * @date 2018/10/26
 * @time 下午11:05
 * @修改记录 <pre>
 * 版本       修改人         修改时间         修改内容描述
 * --------------------------------------------------
 * <p>
 * --------------------------------------------------
 * </pre>
 */


public class ChengJiaoFragment extends BaseFragment {


    Unbinder unbinder;
    @BindView(R.id.fragment_wei_tuo_recy)
    RecyclerView mFragmentWeiTuoRecy;
    @BindView(R.id.fragment_cheng_jiao_item_price)
    TextView mFragmentChengJiaoItemPrice;

    @BindView(R.id.zhang)
    TextView zhang;
    private TradesAdapter mTradesAdapter;
    private String mContract_id;
    private String mPrice_float_num;
    private String mContract_anchor;
    private boolean mIsVisibleToUser;
    private FuturesKLineActivity mActivity;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_cheng_jiao_item, null);
        unbinder = ButterKnife.bind(this, view);
        return view;

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        zhang.setText(getString(R.string.shuliang_zhang));
        mActivity = (FuturesKLineActivity) getActivity();
        mContract_id = mActivity.mContract_id;
        mPrice_float_num = mActivity.mPrice_float_num;
        mContract_anchor = mActivity.mContract_anchor;
        if (mFragmentChengJiaoItemPrice != null) {
            mFragmentChengJiaoItemPrice.setText(getString(R.string.k_price) + "(" + mContract_anchor + ")");
        }
        initRecy();
        getInfo();

        initListener();

    }

    private void initRecy() {
        mFragmentWeiTuoRecy.setLayoutManager(new LinearLayoutManager(getActivity()));
        mTradesAdapter = new TradesAdapter(null);
    }

    private void initListener() {


    }


    @Override
    public void setData() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    private void getInfo() {

        SharedPreferencesUtil sharedPreferences = SharedPreferencesUtil.getSharedPreferences(getActivity());
        PreferencesCookieStore cookieStore = CookieTool.getCookieStore(getActivity(), sharedPreferences);
        RequestParams params = new RequestParams();
        params.addQueryStringParameter("contract_id", mContract_id);
        params.addHeader("myclient", "android");
        int num = (int) ((Math.random() * 9 + 1) * 100000);

        new HttpUtils().configCookieStore(cookieStore).send(HttpRequest.HttpMethod.GET, Config.TRADES + "?rnd=" + String.valueOf(num), params, new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {

                Log.i("wekfwkeufwambdtqie", responseInfo.result);
                mPrice_float_num = mActivity.mPrice_float_num;
                try {
                    JSONObject jsonObject = new JSONObject(responseInfo.result);

                    if (jsonObject.getString("code").equals("1")) {
                        String result = responseInfo.result;
                        Gson gson = new Gson();
                        TradeInfo tradeInfo = gson.fromJson(result, TradeInfo.class);
                        List<TradeInfo.DataBean.ListBean> list = tradeInfo.getData().getList();

                        if (mFragmentWeiTuoRecy != null && mTradesAdapter != null) {
                            mFragmentWeiTuoRecy.setAdapter(mTradesAdapter);
                            mTradesAdapter.SetTyee("1");
                            if (!TextUtils.isEmpty(mPrice_float_num)) {
                                mTradesAdapter.setPriceNumber(mPrice_float_num);
                                mTradesAdapter.setNumber(mPrice_float_num);
                                mTradesAdapter.setAumontNumber("0");
                            }

                            mTradesAdapter.setNewData(list);
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();

                }

            }

            @Override
            public void onFailure(HttpException error, String msg) {

            }

        });

    }


    private Handler handler = new Handler() {

    };
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {

            if (mIsVisibleToUser) {

                getInfo();

            }

            handler.postDelayed(this, 5000);
        }
    };


    @Override
    public void onResume() {
        super.onResume();

        handler.postDelayed(runnable, 0);
    }


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {

        super.setUserVisibleHint(isVisibleToUser);
        mIsVisibleToUser = isVisibleToUser;
    }

    @Override
    public void onPause() {
        super.onPause();
        mIsVisibleToUser = false;
    }

}
